Uec3.CheckCertifyWindow = Ext.extend(Ext.app.Module, {
	id : 'checkCertify',
	type : 'common/checkCertify',

	/**
	 * Initialize this module. This function is called at startup (page
	 * load/refresh).
	 */
	init : function(initParams) {
	},
	paramsCollection : new Ext.util.MixedCollection(true),

	getParamsByKey : function(key) {
		return this.paramsCollection.get(key);
	},
	lookUpMessage : function(id) {					
		
			var desktop = Uec3.App.getDesktop();
			desktop.launchWindowByParam("simpleCompanyMessage", {
				        partnerId : '',
						id : id,
						isHiddenButton:true
					});
			
			 
	},
	addPartner : function(companyId) {
		var desktop = Uec3.App.getDesktop();
		var partner_ds=this.getParamsByKey("partner_ds");
		Ext.Ajax.request({
			url : Uec3.App.Constants.homeUrl
					+ '/partner/query_partner!add_Partner.do',
			success : function(res) {
                  
				var result = Ext
						.decode(res.responseText);
				if (result.success == "OK") {

					 desktop.launchWindowByParam("partner_add",{companyId : companyId,partner_ds:partner_ds});
					
				} else {
                         if(result.failure=="isSelf"){
                        	 
                     		Ext.Msg
							.alert("提示",
									"您好,不能加自己为客户");                      	 
                         }
			           if(result.failure=="alreadyPartner"){
			        	   
			         		Ext.Msg
							.alert("提示",
									" 您好,该公司已经是你的客户,不能重复增加,请到客户列表查看");   
			        	   
			           }

				}
			},

			failure : function(res) {

				Ext.Msg.alert('失败', "网络出现异常，请重先连接");

			},

			params : {
				companyId : companyId
			}

		});
		
  },
    requestAddPartner:function(companyId){
	  
	  var companyMessage_ds=this.getParamsByKey("companyMessage_ds");	  
	  var partner_ds=this.getParamsByKey("partner_ds");
	  var partnerId=this.getParamsByKey("partnerId");
	  var record = companyMessage_ds.getById(companyId);
	  
		if (companyId == 0 || companyId == "") {
			
			Ext.Msg.alert("提示",
					"请先点击检查认证按钮,确认对方已是认证公司,才能进行这步操作")

			return;
		}	
		checkCertify(partner_ds,null,null,partnerId,companyId,record.get("name"),companyId);
     },
   
  
	
	getComponents : function(params) {

		var companyMessage_grid;
		var companyMessage_ds;
		var checkCertifyForm;
	function renderDesc(value, cellmeta, record, rowIndex, columnIndex,
				store) {
           
		var id = record.get("id");
		
			var str = "<a style='font-size:12px' href='javascript:Ext.CUR_APP.getModule(\"checkCertify\").lookUpMessage("
					+ id + ");'>查看信息</a>"+
					"<a style='font-size:12px;margin-left:20px' href='javascript:Ext.CUR_APP.getModule(\"checkCertify\").addPartner("
						+ id + ");'>加为往来单位</a>";

			return str;

		};
		
		function renderOperation(value, cellmeta, record, rowIndex, columnIndex,
				store) {
           
		var id = record.get("id");
		
			var str = "<a style='font-size:12px' href='javascript:Ext.CUR_APP.getModule(\"checkCertify\").lookUpMessage("
					+ id + ");'>查看信息</a>"+
					"<a style='font-size:12px;margin-left:20px' href='javascript:Ext.CUR_APP.getModule(\"checkCertify\").requestAddPartner("
						+ id + ");'>请求加为往来单位</a>";

			return str;

		};
		
		
			var getAndRestoreCheckIsCertify = function() {

			companyMessage_ds = new Ext.data.Store({
						proxy : new Ext.data.HttpProxy({
									url : Uec3.App.Constants.homeUrl+ '/partner/query_partner!searchCompany.do'
								}),
						baseParams : {
						},
						reader : new Ext.data.JsonReader({
									totalProperty : 'totalProperty',
									root : 'root'
								}, [ {
											name : 'id'
										}, {
											name : 'name'
										}, {
											name : 'contactorIdentityCode'
										}, {
											name : 'taxCode'
										}

								]),
						remoteSort : true
					});
		};	
		
		
		
		var componentCheckIsCertifysInits = function() {
		
		
           var companyMessage_colModel = new Ext.grid.ColumnModel([
					new Ext.grid.RowNumberer(), {
						header : "ID",
						width : 60,
						dataIndex : 'id',
						hidden : true
					}, {
						header : "公司名称",
						width : 80,
						dataIndex : 'name'
					},{
						header : "法人身份证",
						width : 80,
						dataIndex : 'contactorIdentityCode'
					},{
						header : "税号",
						width : 80,
						dataIndex : 'taxCode'
					},{
						header : "操作",
						width : 80,
						dataIndex : 'desc',
						align : 'center',
						renderer : renderOperation
					}

			]);
           
           //检查是否认证按钮
			var checkButton = new Ext.Button(

					{   
						text : '检查',
						width:80,
						iconCls : "icon-save",
						handler : function() {
					          // var queryParam = checkCertifyForm.getForm().findField("checkCondition").getValue().inputValue;
						        var queryParam= checkCertifyForm.getForm().getValues();

							    companyMessage_ds.setBaseParam("queryParam",Ext.encode(queryParam));
						        companyMessage_ds.load({
									 callback: function(records,options,success){
			
							         if(success){
							        	 
							        	      if(!records.length){

										        	 Ext.Msg.alert('提示',"不存在该认证公司");							        	    	  
							        	      }

							         }else{
							        	 
							        	 
							        	 Ext.Msg.alert('提示',"网络连接异常...请稍候再试");
							         }			  
								
							 }		
						
						});
						        
							}

					});
           
			companyMessage_grid = new Ext.grid.GridPanel(

					{
						// title:'部门信息列表',
						region : 'center',
						iconCls : 'icon-grid',
						enableColumnMove : false,
						enableColumnMoveResize : false,
						stripeRows : true,
						height : 600,
						loadMask : {
							msg : "...加载数据中"
						},
						store : companyMessage_ds,
						columnLines : false,
						cm : companyMessage_colModel,
						sm : new Ext.grid.RowSelectionModel({
									singleSelect : true
								}),
						viewConfig : {
							forceFit : true,
							columnsText : '显示的列',
							scrollOffset : 20,
							sortAscText : '升序',
							sortDescText : '降序'
						},
						bbar : new Ext.PagingToolbar({
									pageSize : 10,
									store : companyMessage_ds,
									display : true,
									displayInfo : true,
									displayMsg : '第 {0} - {1} 条  共 {2} 条',
									emptyMsg : "没有记录",
									items : [
									]
								})

					});
			checkCertifyForm = new Ext.form.FormPanel({
						labelAlign : 'right',
						frame : true,
						labelWidth : 60,
						loadingText : '加载数据中...',
						buttonAlign : 'right',
						url : '',
					items:[{
									layout : 'column',
									xtype : 'fieldset',	
									anchor:'100%',
									border : false,
						items : [
									
								{   columnWidth:0.50,
									xtype : 'radiogroup',
									style : "color:#FF8C00;",
									fieldLabel : '检查条件',
									blankText : "检查条件必须填写",
									allowBlank : false,
									name : 'checkCondition',
									items : [{
												boxLabel : "用法人/负责人身份证号检查",
												name : "checkCondition",
												inputValue : 1,
												checked : true
											}, {
												boxLabel : "用税号检查",
												name : "checkCondition",
												inputValue : 2
											}]
								},{
									columnWidth:0.20,
									xtype:'textfield',
									name:'conditionText',
									regex:/^[0-9]+$/,
									regexText:'请输入数字'
								},{columnWidth:0.25,html:"查找条件:法人身份证,税号",cls : "x-form-field",style:'color:#cc0000;margin-top:4px'},checkButton
						]
					},
					
					companyMessage_grid
				],
		buttons :[{
					text : '关闭',
					iconCls : "icon-close",
					handler : function() {

			             checkCertifyForm.ownerCt.close();

					},
					style : "margin-left:20px"
				}]
			});
			

		}

		function checkIsCertifysControlEvent() {
		
		   if(params.partner_form){
					
			 var partner_form=params.partner_form;
			   checkCertifyForm.getForm().findField("checkCondition").on(
					"change", function(radioGroup, radio) {
						
						if (radio.getRawValue() == 1) {

							var contactorIdentityCode=partner_form.getForm().findField("contactorIdentityCode").getValue();
		            		 checkCertifyForm.getForm().findField("conditionText").setValue(contactorIdentityCode);
							
						} else if(radio.getRawValue() == 2){

							 var taxCode=partner_form.getForm().findField("taxCode").getValue();
		            		 checkCertifyForm.getForm().findField("conditionText").setValue(taxCode);
						}else{
		            		 
		            		 alert("error operation");
		            	 }
					})
		     }	
		
		}
        getAndRestoreCheckIsCertify();
		componentCheckIsCertifysInits();

		checkIsCertifysControlEvent();
		this.paramsCollection.add("companyMessage_ds", companyMessage_ds);
		this.paramsCollection.add("checkCertifyForm", checkCertifyForm);
		return checkCertifyForm;
	},
	initEvent : function() {
		var module = this;
		var win = module.getParamsByKey("win");
		var partner_form = module.getParamsByKey("partner_form");
		var checkCertifyForm = module.getParamsByKey("checkCertifyForm");
		win.on('show', function() {
                        
			            if(partner_form){
			            	var value = checkCertifyForm.getForm().findField("checkCondition").getValue().inputValue;
			            	 if(value==1){
			            		 var contactorIdentityCode=partner_form.getForm().findField("contactorIdentityCode").getValue();
			            		 checkCertifyForm.getForm().findField("conditionText").setValue(contactorIdentityCode);
			            		 
			            	 }else if(value==2){
			            		 
			            		 var taxCode=partner_form.getForm().findField("taxCode").getValue();
			            		 checkCertifyForm.getForm().findField("conditionText").setValue(taxCode);
			            		 
			            	 }else{
			            		 
			            		 alert("error operation");
			            	 }
			            	
			            	checkCertifyForm.getForm().findField("conditionText").hide();
			            	
			            	
			            }	
			
				}, module, {
					single : true
				});
	},
	/**
	 * Create this modules window here.
	 */
	createWindow : function(params) {
		
		var desktop = this.app.getDesktop();
		var win = desktop.getWindow('checkCertify');
		this.paramsCollection.add("partner_form", params.partner_form);
		this.paramsCollection.add("partnerId", params.id);
		this.paramsCollection.add("partner_ds", params.partner_ds);
		var module = this;
		if (!win) {
			win = desktop.createWindow({
						id : "checkCertify",
						title : '搜索认证公司',
						width :850,
						height :500,
						iconCls : 'acc-icon',
						shim : false,
						animCollapse : false,
						modal : true,
						plain : true,
						constrainHeader : true,
						maximizable : false,
						resizable : false,
						listeners:{"beforeshow":function(){
							
						}
			},
						taskbuttonTooltip : "搜索认证公司",
						layout : "fit",
						tools : [{
									id : 'refresh',
									handler : Ext.emptyFn,
									scope : this
								}],
						items : [this.getComponents(params)]
					});

			module.paramsCollection.add("win", win);
			module.initEvent();
		}
		win.show();

	}
});